<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Api\Controller;
use Think\Controller;

/**
 * 前台公共控制器
 * 为防止多分组Controller名称冲突，公共Controller名称统一使用分组名称
 */
class LoginController extends Controller {

	/* 空操作，用于输出404页面 */
	// public function _empty(){
	// 	echo 404; //TODO:完成404页面
	// }
	// TODO: 为了调试方便，暂时注释

    protected function _initialize(){
        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置

        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }

    }

	/* 用户登录检测 */
	public function login(){
        //判断有没有登陆
         if(session('member')){
             $this->redirect('Index/index');
         }
        if($_COOKIE['member_id']){
            $arr=M('tmember')->where(array('id'=>$_COOKIE['member_id']))->find();
            session('member',$arr); //存入session
            setcookie('member_id',$arr['id'],time()+7*24*60*60,'/');
            $this->redirect('Index/index');
        }

        if($_POST['phone']&&$_POST['pass']){
            $phone=$_POST['phone'];
            $pass=$_POST['pass'];
            $model=M('tmember');
            $arr=$model->where(array('user'=>$phone))->find();
            if($arr){
                if($arr['start']=='3'){
                    $this->ajaxReturn(array('type'=>2,'msg'=>'你已被拉黑'));
                }
                //登陆
                if($arr['pass']==$pass){
                    session('member',$arr); //存入session
                    setcookie('member_id',$arr['id'],time()+7*24*60*60,'/');
                    $this->ajaxReturn(array('type'=>1,'msg'=>'登陆成功'));
                }else{
                    $this->ajaxReturn(array('type'=>2,'msg'=>'密码错误'));
                }
            }else{
                //注册
                $arr=array();
                $arr['user']=$phone;
                $arr['phone']=$phone;
                $arr['pass']=$pass;
                $arr['start']='1';
                $arr['time']=date('Y-m-d H:i:s',time());
                $id=$model->add($arr);
                if($id){
                    $arr['id']=$id;
                    session('member',$arr); //存入session
                    setcookie('member_id',$arr['id'],time()+7*24*60*60,'/');
                    $this->ajaxReturn(array('type'=>1,'msg'=>'注册成功'));
                }else{
                    $this->ajaxReturn(array('type'=>2,'msg'=>'失败'));
                }
            }
        }
        $this->assign('applogo',C('TBD_LOGO'));
        $this->display();
	}

    //退出登陆
    public function tc(){
        session('member',null);
        setcookie('member_id','',time()-10,'/');
        $this->redirect('Login/login');
    }



}
